package com.blocks;

import java.util.LinkedList;
import java.util.List;

import com.blocks.blocks.ActionBlock;
import com.blocks.blocks.BatteryBlock;
import com.blocks.blocks.Block;
import com.blocks.blocks.MotorBlock;
import com.blocks.blocks.PowerBlock;
import com.blocks.model.BlockSides;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.joints.HingeJoint;
import com.jme3.collision.CollisionResult;
import com.jme3.math.Vector3f;

public class BlockHandler {
//	
//	private List<Clusters> clusterList;
//	
//	public BlockHandler()
//	{
//		clusterList = new LinkedList<Clusters>();
//		
//	}
//	
//	public boolean addCluster(Clusters clust)
//	{
//		return clusterList.add(clust);
//			
//	}
//	
//	public void traversePowerBlockinClusters()
//	{
//		// get list of powerblocks from clust
//
//		// loop through powerblocks
//		// for each powerblock
//		// recursePowerblock(x,0,x.powerlimit)
//
//		for (Clusters clust: clusterList)
//		{
//			if (clust.isChanged() == true)
//			{
//				List<PowerBlock> templist = clust.getPowerList();
//		
//				for (PowerBlock powerblock: templist)
//				{
//					recursePowerblock(powerblock, 0, powerblock.powerlimit );
//					
//					// TODO:
//					// traverse through cluster and set all blocks traversed = false
//					// have to do this per powerblock or else we might miss some
//					// still not set on this
//				}
//				
//				clust.setChanged(false);
//			}
//		}
//	
//		//return true;
//	}
//	
//	// still thinking about this
//	private boolean recursePowerblock(Block currentblock, int currentpower, int powerlimit)
//	{
//		if(currentblock == null){
//			return false;
//		}
//		// start at power block
//		
//		// if current power > powerlimit
//		// return true;
//		
//		if (currentpower > powerlimit)
//		{
//			return true;
//		}
//		
//		// if currentblock = nonconductiveblock
//		// return true;
//		
//		// TODO:
//		// create non conductive blocks
//		
//		// if currentblock = actionblockP
//		// currentblock.powered = true
//		
//		if (ActionBlock.class.isAssignableFrom(currentblock.getClass()))
//		{
//			((ActionBlock)currentblock).setPowered(true);
//		}
//		
//		recursePowerblock(currentblock.getBlockAbove(), currentpower + 1, powerlimit);
//		recursePowerblock(currentblock.getBlockBelow(), currentpower + 1, powerlimit);
//		recursePowerblock(currentblock.getBlockLeft(), currentpower + 1, powerlimit);
//		recursePowerblock(currentblock.getBlockRight(), currentpower + 1, powerlimit);
//		recursePowerblock(currentblock.getBlockFront(), currentpower + 1, powerlimit);
//		recursePowerblock(currentblock.getBlockBack(), currentpower + 1, powerlimit);
//		
//		return true;
//	}
//	
//	public boolean weldBlocks(Block blk1, Block blk2, BlockSides sideblock1, BlockSides sideblock2) { //, CollisionResult cr) {
//		
//		// TODO:
//		// Fixed hardcoded values for BlockSides
//		// Add handlers for each side
//		
//		// TODO:
//		// Needs to be passed in a collision check
//		// CollisionResult cr
//		// Vector3f targetPosition = new Vector3f(cr.getGeometry().getWorldBound().getCenter().add(cr.getContactNormal().mult(CUBE-SIZE)));
//		// Unfortunately this won't let us know which side the block landed on
//		// so we can't set it logically
//				
//		// Second option:
//		//		
//		// a) calculate a CollisionResult
//		// b) read the triangle-index of the collision
//		// c) you must “know” the internal mesh structure to say index xxx is side 0,1,2,3,4 or 5
//		// d) you can get the triangle for the index … but remember a jme-box have always 2 triangles per side (here I give up *g* )
//			
//		// First we check to see if block1 or block2 are currently part of a cluster
//		if (blk1.getClust() == null && blk2.getClust() == null)
//		{
//			// neither block are part of a cluster
//			
//			// add both to a new cluster
//			Clusters newclust = new Clusters();
//			newclust.init();
//			newclust.addBlock(blk1);
//			
//			blk1.setClust(newclust);
//			blk1.setBlockAbove(blk2);
//			blk2.setBlockBelow(blk1);
//			newclust.setChanged(true);
//			clusterList.add(newclust);
//			
////			HingeJoint joint = new HingeJoint(
////			blk1.getControl(RigidBodyControl.class),
////			blk2.getControl(RigidBodyControl.class), new Vector3f(
////					0f, 4.0f, 0f), new Vector3f(0f, -1.0f, 0f),
////			Vector3f.UNIT_Z, Vector3f.UNIT_Y);
////	
////			ScriptBlocksApplication.getInstance().getPhysicsSpace().add(joint);
//		}
//		else if (blk1.getClust() != null && blk2.getClust() == null)
//		{
//			//block1 is part of a cluster
//			Clusters tempclust = blk1.getClust();
//			
//			blk1.setBlockAbove(blk2);
//			blk2.setBlockBelow(blk1);
//			tempclust.addBlock(blk2);
//			tempclust.setChanged(true);
//			
////			HingeJoint joint = new HingeJoint(
////			blk1.getControl(RigidBodyControl.class),
////			blk2.getControl(RigidBodyControl.class), new Vector3f(
////					0f, 4.0f, 0f), new Vector3f(0f, -1.0f, 0f),
////			Vector3f.UNIT_Z, Vector3f.UNIT_Y);
////	
////			ScriptBlocksApplication.getInstance().getPhysicsSpace().add(joint);
//			//return true;
//		}
//		else if (blk1.getClust() == null && blk2.getClust() != null)
//		{
//			//block2 is part of a cluster
//			Clusters tempclust = blk2.getClust();
//			
//			blk1.setBlockAbove(blk2);
//			blk2.setBlockBelow(blk1);
//			tempclust.addBlock(blk1);
//			tempclust.setChanged(true);
////			
////			HingeJoint joint = new HingeJoint(
////			blk2.getControl(RigidBodyControl.class),
////			blk1.getControl(RigidBodyControl.class), new Vector3f(
////					0f, 4.0f, 0f), new Vector3f(0f, -1.0f, 0f),
////			Vector3f.UNIT_Z, Vector3f.UNIT_Y);
////	
////			ScriptBlocksApplication.getInstance().getPhysicsSpace().add(joint);
//		}
//		else
//		{
//			//both are part of a cluster; we can't handle that yet!
//			return false;
//		}
//		
//		return true;
////		batteryBlock = new BatteryBlock("TWO", assetManager, 1);
////		batteryBlock.init();
////		//this.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(1, 1, 1)), 1));
////		batteryBlock.getControl(RigidBodyControl.class).setMass(0.0f);
////		// batteryBlock.setLocalTranslation(2.0f, 0.0f, 0.0f);
////		rootNode.attachChild(batteryBlock);
////		getPhysicsSpace().add(batteryBlock);
////		batteryBlock.getControl(RigidBodyControl.class).setPhysicsLocation(
////				new Vector3f(50.0f, -23.0f, 0.0f));
////
////		motorBlock = new MotorBlock("THREE", assetManager);
////		motorBlock.init();
////		motorBlock.getControl(RigidBodyControl.class).setMass(100.0f);
////		rootNode.attachChild(motorBlock);
////		getPhysicsSpace().add(motorBlock);
////		motorBlock.getControl(RigidBodyControl.class).setPhysicsLocation(
////				new Vector3f(0.0f, 0.0f, 0.0f));
////
////		HingeJoint joint = new HingeJoint(
////				motorBlock.getControl(RigidBodyControl.class),
////				batteryBlock.getControl(RigidBodyControl.class), new Vector3f(
////						0f, 4.0f, 0f), new Vector3f(0f, -1.0f, 0f),
////				Vector3f.UNIT_Z, Vector3f.UNIT_Y);
////		
////		getPhysicsSpace().add(joint);
////		joint.enableMotor(true, 1000.0f, 1000.0f);
//
//	}

}
